<template>
	<view>
		<u-navbar :title="this.key == 'content' ? '编辑方案正文内容' : '编辑方案简介'" placeholder :titleWidth="'80%'" :titleStyle="{fontSize: '14px', color: '#ffffff'}" :bgColor="'#52B36D'" :leftIconColor="'#ffffff'">
			<view class="u-nav-slot" slot="left">
				<u-icon name="arrow-left" :color="'#ffffff'" size="14" @click="leftClick" />
			</view>
			<view class="u-nav-slot" slot="right">
				<view class="Grid Grid--justifyCenter Grid--center" style="flex-direction: column;" @click="saveHtml">
					<u-button text="保存"></u-button>
				</view>
			</view>
		</u-navbar>
		<view class="content">
			<male-editor :uploadFile="upFile" v-model="html" :placeholder="placeholder"></male-editor>
			<!-- <button @click="gethtml()">获取HTML</button>
			<button @click="sethtml()">修改HTML</button> -->
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import navConfig from './config/navConfig.js'
	import {
		addFile
	} from '@/service/index.js'

	export default {
		data() {
			return {
				navConfig,
				key: '',
				form: {},
				placeholder: '请输入内容',
				html: ``,
				// fileServer: {
				// 	url: "",
				// 	name: "imgFile",
				// 	header: {
				// 		Authorization: ''
				// 	},
				// 	formData: {
				// 		userid: ""
				// 	},
				// 	fileNum: 1,
				// 	sizeType: ['original', 'compressed'],
				// 	sourceType: ['album', 'camera'],
				// 	inImgData: true //返回信息是否插入图片标签data中
				// }
			}
		},
		components: {

		},
		computed: {
			...mapState({
				userInfo: state => state.login.userInfo
			}),
		},
		onLoad(option) {
			this.key = option.key
			let pages = getCurrentPages();
			let page = pages[pages.length - 1] // 获取当前页面的数据，包含页面路由
			console.log(page.route)
			if (page.route === 'pages/index/editor-html/index') {
				try {
					this.form = uni.getStorageSync('form');
					this.$nextTick(() => {
						if (this.form[this.key]) this.html = this.form[this.key]
					})
				} catch (e) {
					// error
				}
			}
		},
		mounted() {
			
		},
		onReady() {
			
		},
		methods: {
			setPosition(e) {
				let x = e.touches[0].pageX;
				let y = e.touches[0].pageY;
				this.site = {
					x,
					y
				}
				// console.log(e);
			},
			gethtml() {
				uni.showModal({
					content: this.html
				})
			},
			sethtml() {
				this.html = '<b style="color:#33ff14">内容已经被修改</b>'
			},
			upFile(callback) {
				uni.chooseImage({
					sizeType: 'compressed',
					count: 9,
					success: res => {
						// H5
						console.log(res.tempFiles)
						uni.showLoading({
							title: '上传中'
						});
						(async () => {
							for (let file of res
									.tempFiles) {
								// 依次上传
								const result =
									await addFile({
										// #ifdef H5 || APP-PLUS
										timeout: 60000, // H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)
										// #endif
										file: file,
										name: 'file'
									})
								if (result.code ===
									200) {
									callback({
										src: result.data.file_url
									})
									console.log(result
										.data
										.file_url)
								}
							}
						})().then(res => {
							uni.hideLoading()
						})
						// for (let item of res.tempFilePaths) {
									
						// 	uni.uploadFile({
						// 		url: this.fileServer.url,
						// 		name: this.fileServer.name,
						// 		formData: this.fileServer.formData,
						// 		header: this.fileServer.header,
						// 		filePath: item,
						// 		success: (rs) => {
						// 			console.log(rs.data);
						// 			// console.log(JSON.parse() rs.data.Data);
									
						// 			let imgobj = {
						// 				src: 'http://localhost:21825/' + JSON.parse(rs.data)
						// 					.Data,
						// 				data: rs.data
						// 			}
						// 			callback(imgobj)
						// 		},
						// 		fail(rs) {
						// 			uni.showModal({
						// 				content: '网络错误，请检查你的网络',
						// 				showCancel: false
						// 			});
						// 			// reject({
						// 			// 	statuscode: 999,
						// 			// 	codemsg: '网络错误，请检查你的网络'
						// 			// })
						// 		}
						// 	})
									
						// }
					}
				})
			},
			leftClick() {
				uni.navigateBack({});
			},
			saveHtml() {
				setTimeout(() => {
					var content = this.html
					this.form[this.key] = content
					// 将方案内容存储在本地
					try {
						uni.setStorageSync('form', this.form);
					} catch (e) {
						// error
					}
					
					uni.reLaunch({
						url: `/pages/index/index?appid=${this.userInfo.appid}&index=1`
					});
				}, 50)
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-top: 80px;
	}
</style>